﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using ParserGenerator.Extensions;
using ParserGenerator.GrammarConstruction;
using ParserGenerator.ScannerConstruction;

namespace ParserGenerator.GrammarAnalysis
{
    [Serializable]
    internal sealed class LexicalPrecedenceResolution : ReadOnlyDictionary<ScannerAutomatonState, GrammarTerminalSymbol>
    {
        internal LexicalPrecedenceResolution(Dictionary<ScannerAutomatonState, GrammarTerminalSymbol> dict)
            : base(dict)
        {
        }

        public bool Equals(LexicalPrecedenceResolution other)
        {
            return other != null && Dictionary.DictionaryEquals(other.Dictionary);
        }

        public sealed override bool Equals(object obj)
        {
            return obj is LexicalPrecedenceResolution && Equals((LexicalPrecedenceResolution)obj);
        }

        public sealed override int GetHashCode()
        {
            return Dictionary.GetDictionaryHashCode();
        }

        public sealed override string ToString()
        {
            var items = from t in Dictionary orderby t.Key.Number select t.Key + ": " + t.Value;
            return "{" + string.Join(", ", items) + "}";
        }
    }
}
